﻿using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;

public partial class treeViewDirect : TreeView
{
    public treeViewDirect()
    {
        InitializeComponent();

        init();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
    }

    private void init_ImageList()
    {
        //*********************************************************
        //图片list
        //*********************************************************

        #region

        ImageList imageList = new ImageList();

        string strPath = Path.Combine(Environment.CurrentDirectory, "Xml/Img.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(strPath);

        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;

        string ImageKey = "";
        string SelectedImageKey = "";
        string ImageKeyPath = "";
        string SelectedImageKeyPath = "";

        foreach (XmlNode xn in nodeList)
        {
            if (xn.Name == "row")
            {
                foreach (XmlNode xn1 in xn.ChildNodes)
                {
                    if (xn1.Name == "ImageKey")
                    {
                        ImageKey = xn1.InnerText.Trim();
                    }
                    else if (xn1.Name == "SelectedImageKey")
                    {
                        SelectedImageKey = xn1.InnerText.Trim();
                    }
                    else if (xn1.Name == "ImageKeyPath")
                    {
                        ImageKeyPath = xn1.InnerText.Trim();
                    }
                    else if (xn1.Name == "SelectedImageKeyPath")
                    {
                        SelectedImageKeyPath = xn1.InnerText.Trim();
                    }
                    else
                    {
                        continue;
                    }
                }
                if (ImageKey == ".java")
                {
                }
                try
                {
                    imageList.Images.Add(ImageKey, new Bitmap(ImageKeyPath.Trim()));
                    imageList.Images.Add(SelectedImageKey, new Bitmap(SelectedImageKeyPath.Trim()));
                }
                catch (Exception ee)
                {
                }
            }
        }
        this.ImageList = imageList;

        #endregion
    }

    private void init()
    {
        //首先初始化图片控件
        init_ImageList();

        //初始化1层节点
        //initRootNode();

        ///pai xu
        this.TreeViewNodeSorter = new NodeSorter();
        this.LabelEdit = false;
    }

    /// <summary>
    /// 初始化节点
    /// </summary>
    public void initRootNode()
    {
        //*********************************************************
        //~~初始化节点
        //*********************************************************
        this.Nodes.Clear();

        //初始化
        //XmlNodeList nodeList = initGetXmlPath();
        XmlDocument doc = new XmlDocument();
        string strPath = "Xml/Path.xml";
        doc.Load(strPath);
        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;

        string strAllPath = "";
        string strDirPath = "";
        string strFlag = "";
        foreach (XmlNode xn in nodeList)
        {
            if (xn.Name == "row")
            {
                //initSetDirectory(ref strAllPath, ref strDirPath, ref strFlag, xn);
                strAllPath = xn["DirectoryRoot"].InnerText;
                strAllPath += xn["Directory"].InnerText;
                if (xn["Directory"].InnerText == "")
                {
                    strDirPath = xn["DirectoryRoot"].InnerText;
                }
                else
                {
                    strDirPath = xn["Directory"].InnerText;
                }
                strFlag = xn["Expand"].InnerText;

                //添加
                if (!System.IO.Directory.Exists(strAllPath)) { continue; }

                TreeNode subNode = initGetDirectoryNode(strAllPath, strDirPath, strFlag);
                this.Nodes.Add(subNode);
            }
        }
    }

    private TreeNode initGetDirectoryNode(string strAllPath, string strDirPath, string strFlag)
    {
        TreeNode subNode = new TreeNode(strDirPath);

        //0 fullpath  1 d or f
        ArrayList arrayListTag = new ArrayList();

        arrayListTag.Add(strAllPath);
        arrayListTag.Add("d");

        subNode.ImageKey = "dir";
        subNode.SelectedImageKey = "dir";
        subNode.Tag = arrayListTag;
        if (strFlag == "1")
        {
            this.BeginUpdate();
            int iDirectories = TreeViewHelper.AddDirectories(subNode);
            int iFiles = TreeViewHelper.AddFiles(subNode);
            this.EndUpdate();

            subNode.Expand();
        }
        return subNode;
    }

    private static void initSetDirectory(ref string strAllPath, ref string strDirPath, ref string strFlag, XmlNode xn)
    {
        //读取
        strAllPath = xn["DirectoryRoot"].InnerText;
        strAllPath += xn["Directory"].InnerText;
        strDirPath = xn["Directory"].InnerText;
        strFlag = xn["Expand"].InnerText;

        //foreach (XmlNode xn1 in xn.ChildNodes)
        //{
        //    if (xn1.Name == "DirectoryRoot")
        //    {
        //        strAllPath = xn1.InnerText;

        //    }
        //    else if (xn1.Name == "Directory")
        //    {
        //        strAllPath += xn1.InnerText;
        //        strDirPath = xn1.InnerText;
        //    }

        //    else if (xn1.Name == "Expand")
        //    {
        //        strFlag = xn1.InnerText;

        //    }

        //}
    }

    private static XmlNodeList initGetXmlPath()
    {
        XmlDocument doc = new XmlDocument();
        string strPath = "Xml/Path.xml";
        doc.Load(strPath);

        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;
        return nodeList;
    }
}